package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.pojo.UserRecode;
import com.atguigu.gmall.activity.service.SeckillOrderService;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/***
 * 秒杀订单controller层
 */
@RestController
@RequestMapping("/api/seckillOrder")
public class ApiSeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;

    /**
     * 主动取消订单 / 超时取消订单
     * @return
     */
    @PutMapping("/cancel")
    public Result cancel(){
        seckillOrderService.cancelOrder(null);

        return Result.ok();
    }

    /**
     * 获取用户排队信息
     * @return
     */
    @GetMapping("/getUserRecode")
    public Result getUserRecode(){
        //1 调用业务层方法
        UserRecode userRecode = seckillOrderService.getUserRecode();

        //2 响应数据
        return Result.ok(userRecode);
    }

    /**
     * 添加订单
     * @param dateTime
     * @param seckillGoodsId
     * @param num
     * @return
     */
    @PostMapping("/addSeckillOrder")
    public Result addSeckillOrder(String dateTime, String seckillGoodsId, Integer num){
        //1 参数校验
        if(StringUtils.isEmpty(dateTime) || StringUtils.isEmpty(seckillGoodsId) || num == null){
            throw new GmallException("参数非法，秒杀下单失败！！！",null);

        }

        //2 调用业务层方法，进行添加
        UserRecode userRecode = seckillOrderService.addSeckillOrder(dateTime, seckillGoodsId, num);

        //3 响应结果
        return Result.ok(userRecode);
    }
}
